package org.smile.db.handler;

import org.smile.collection.LinkedHashMap;

import java.util.Map;

public class ExpandableHumpResultSetMap extends HumpResultSetMap{

    @Override
    public Object put(String key, Object value) {
        String realKey = convertKey(key);
        int index=realKey.indexOf(".");
        if(index>0){
            String subKey=realKey.substring(0,index);
            Map<String,Object> obj=(Map)getUseRealKey(subKey);
            if(obj==null){
                obj=new LinkedHashMap<>();
                putUseRealKey(subKey,obj);
            }
            return put(obj,key.substring(index+1),value);
        }else {
            this.realKeyMap.put(key, realKey);
            return putUseRealKey(realKey, value);
        }
    }

    protected  Object put(Map<String,Object> map, String key, Object value){
        int idx=key.indexOf(".");
        if(idx>0){
            String subKey= key.substring(0,idx);
            Map<String,Object> obj=(Map)map.get(subKey);
            if(obj==null){
                obj=new LinkedHashMap<>();
                map.put(subKey,obj);
            }
            return put(obj,key.substring(idx+1),value);
        }else{
            return map.put(key,value);
        }
    }
}
